package com.nlscan.qdkit.ui.publico.art.mvp;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import androidx.viewbinding.ViewBinding;

import com.nlscan.qdkit.ui.publico.R;
import com.nlscan.qdkit.ui.publico.config.PublicoConfig;
import com.nlscan.qdkit.ui.publico.config.PublicoConfigHelper;
import com.nlscan.qdkit.ui.publico.config.PublicoVariate;
import com.nlscan.qdkit.ui.publico.util.AutoMoveHelper;
import com.nlscan.qdkit.ui.publico.util.ScreenUtils;
import com.nlscan.qdkit.ui.publico.util.SnackBarUtil;
import com.nlscan.qdkit.ui.publico.util.SystemUtil;
import com.qing.mvpart.base.QvActivity;
import com.qing.mvpart.mvp.IPresenter;
import com.qing.mvpart.mvp.IView;
import com.quvii.qvlib.util.LogUtil;
import com.quvii.qvlib.util.QvSoftKeyBoardListener;
import com.quvii.qvlib.util.QvToastUtil;

/**
 * 基于MVP封装UI相关的Activity 基类
 * Created by QING on 2023-02-11.
 * -- 1. 沉浸式
 * -- 2023-04-11 2. 手机、平板横竖屏自适应
 * todo 2023-04-11 rename ,避免用错
 */
public abstract class BaseMvpActivity<T extends ViewBinding, P extends IPresenter> extends QvActivity<P> implements IView {
    public static final String BASE_INTENT_BUNDLE = "BASE_INTENT_BUNDLE";

    private AutoMoveHelper autoMoveHelper;

    protected T binding;

    protected abstract T getViewBinding();

    protected void setTopMoveView(View view) {
        if (autoMoveHelper == null) {
            autoMoveHelper = new AutoMoveHelper(view);

            QvSoftKeyBoardListener.SetListener(this, (isShow, height) -> {
                if (isShow) {
                    autoMoveHelper.moveTop();
                } else {
                    autoMoveHelper.moveBottom();
                }
            });
        }
    }

    @Override
    public int getLayoutId() {
        binding = getViewBinding();
        setContentView(binding.getRoot());
        onLayoutComplete(binding.getRoot());
//        RouterCenter.INSTANCE.inject(this);
        return 0;
    }

    @Override
    public boolean useButterKnife() {
        return false;
    }

    @Override
    protected int getStatusBarColor() {
        return R.color.colorPrimary;
    }

    @Override
    protected boolean isDarkFont() {
        return PublicoConfig.APP_IS_TITLE_DARK_FONT;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SystemUtil.setForceDarkNotAllowed(getWindow().getDecorView());
//        if (!AppStatusManager.getInstance().isNormalStart()) {
//            LogUtil.e(TAG, "应用在后台被强杀，重启应用");
//            SystemUtil.restartApp(this);
//            return;
//        }
        listenNavigationState();

    }


    /**
     * 监听NavigationBar的显隐状态
     * <p>
     * 适配三星s8，此时虚拟按钮显示，华为p9因监听无效，VIRTUAL_NAVIGATION_BAR_STATE 始终为 0
     * 该值始终不变，但对应getMetrics获取到的分辨率会自动发生改变
     * 对应三星s8获取getMetrics值始终不变
     */
    private void listenNavigationState() {
        getWindow().getDecorView()
                .setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
                    @Override
                    public void onSystemUiVisibilityChange(int visibility) {
                        // TODO: 2018/4/26 u
                        //适配三星s8，该监听对华为p9无效
                        ScreenUtils.VIRTUAL_NAVIGATION_BAR_STATE = visibility;
                    }
                });
    }


    @Override
    protected boolean onPreStart() {
        //初始化Activity的方向
        PublicoConfigHelper.configDefaultActivityOrientation(this);
        return super.onPreStart();
    }

    @Override
    public void showError(int code, String msg) {
//        QvToastUtil.showS(msg + "\n" + QvSdkErrorUtil.getSdkResult(code));
    }

    protected void showSnackBar(View view, int res) {
        SnackBarUtil.Show(view, getString(res));
    }

    protected void showSnackBar(View view, String info) {
        SnackBarUtil.Show(view, info);
    }

    @Override
    public void showResult(int code) {
//        showMessage(QvSdkErrorUtil.getSdkResult(code));
    }

    @Override
    public void setListener() {

    }

    @Override
    public void processLogic() {

    }

    protected void backToMain() {
//        RouterCenter.INSTANCE.navigationActivity(this, Router.Main.A_MAIN);
        finish();
    }

    protected void backToMain(IntentCallBack callBack) {
        Intent intent = createIntent();
        callBack.onStart(intent);

//        RouterService.Main(service -> service.backMain(mContext, intent));

        finish();
    }

    @Override
    protected void onResume() {
        if (PublicoVariate.isPadMode) {
            hideNavigationBar(null);
        }
        super.onResume();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (autoMoveHelper != null) {
            autoMoveHelper.release();
            autoMoveHelper = null;
        }
    }

    public boolean isEmpty(Object obj) {
        if (obj == null) {
            throw new NullPointerException();
        }
        return TextUtils.isEmpty(getText(obj));
    }

    public boolean isEmpty(String str) {
        return TextUtils.isEmpty(str);
    }

    /**
     * 获取 editTest，textView,checkbox和button的文字
     */
    public String getText(Object obj) {
        if (obj instanceof TextView) {
            return ((TextView) obj).getText().toString();
        }
        LogUtil.e("类型转换异常");
        return "";
    }

    public void setText(Object obj, String str) {
        if (!isEmpty(str) && (obj != null)) {
            if (obj instanceof TextView) {
                ((TextView) obj).setText(str);
            }
        }
    }

    protected LayoutInflater getInflater() {
        return (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public void toast(String msg) {
        QvToastUtil.showS(msg);
    }

    public void toast(int strId) {
        QvToastUtil.showS(strId);
    }

    protected Activity getContext() {
        return mContext;
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
//        RouterCenter.INSTANCE.inject(this);
    }

    protected Intent createIntent() {
        return createIntent(BaseMvpActivity.class);
    }
}
